﻿using System;
using System.Web.UI.WebControls;
using Medianamik.Globalization;

namespace Medianamik.UI.Controls
{
    public class FutureDatePicker : DatePicker
    {
        protected CustomValidator _dateValidator = new CustomValidator();

        protected override void CreateControlHierarchy(bool pUseDataSource)
        {
            base.CreateControlHierarchy(pUseDataSource);
            InitializeFutureDateValidator();
        }

        private void InitializeFutureDateValidator()
        {
            _dateValidator.ControlToValidate = ControlToValidateId;
            _dateValidator.ValidateEmptyText = false;
            _dateValidator.ValidationGroup = ValidationGroupName;
            _dateValidator.Display = ValidatorDisplay.None;

            _dateValidator.ServerValidate += ValidateFutureDate;
            _dateValidator.ErrorMessage += Translator.Translate("Medianamik_Controls", "FutureDateMessage");

            Controls.Add(_dateValidator);
        }

        private void ValidateFutureDate(object source, ServerValidateEventArgs args)
        {
            DateTime realDate;

            args.IsValid = DateTime.TryParse(args.Value, out realDate) && 
                (realDate >= DateTime.Today);
        }
    }
}